package com.deyinet.bdcms.tools;

import com.alibaba.fastjson.JSON;
import com.deyinet.bdcms.entity.Syslog;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.entity.Sysuser;
import com.deyinet.bdcms.service.ILogsService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Bacel on 2017/4/5.
 */
public class WebUtilify {
    public static String LoginUserKey = "LoginUserKey";
    public static String ClientCkey = "123kljfd";

    //获取登录用户信息
    public static Sysuser LoginUserinfo(HttpServletRequest request) {
        Sysuser user = new Sysuser();
        HttpSession session = request.getSession();
        Object pObj = session.getAttribute(WebUtilify.LoginUserKey);
        if (pObj != null) {
            user = (Sysuser) pObj;
        }
        return user;
    }

    public static void WriteLog(HttpServletRequest request,Syslog log, ILogsService iLogsService) {
        Sysuser user = WebUtilify.LoginUserinfo(request);
        if (user.getId()!=superUser.getId()||user.getRoleId()!=superUser.getRoleId()){
            iLogsService.insertT(log);
        }
    }


    public static int parseInt(String s, int defaultValue) {
        if (s == null) return defaultValue;
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException x) {
            return defaultValue;
        }
    }

    public static long parseLong(String s, long defaultValue) {
        if (s == null) return defaultValue;
        try {
            return Long.parseLong(s);
        } catch (NumberFormatException x) {
            return defaultValue;
        }
    }



    //输出json返回信息
    public static String ReturnJsoninfo(String errstr) {
        String ret = "";
        String message = "";
        String msgcontents = "";
        if (errstr == null || errstr.length() > 0) {
            message = "nook";
            msgcontents = errstr;
        } else {
            message = "ok";
            msgcontents = "操作成功";
        }
        Map<String, Object> info = new HashMap<>();
        info.put("Message", message);
        info.put("Content", msgcontents);
        ret = JSON.toJSONString(info);
        return ret;
    }
}
